﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.Drawing;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for Util
/// </summary>
public class Util
{
    public Util()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static string RandomString()
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        char ch;
        for (int i = 0; i < 9; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }
        return builder.ToString();
    }

    /// <summary>
    /// Save shot images
    /// </summary>
    /// <param name="maxH"></param>
    /// <param name="maxW"></param>
    /// <param name="url"></param>
    /// <param name="DestinationFolder"></param>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static bool SaveTNShotImage(int maxH, int maxW, string url, string DestinationFolder, string filename)
    {
        System.Drawing.Image fullSizeImg;// = default(System.Drawing.Image);

        string FileName = null;
        int h = 0;
        int w = 0;
        int dif = 0;
        decimal ratio = 0;

        try
        {
            fullSizeImg = System.Drawing.Image.FromFile(url);

        }
        catch (Exception e)
        {
            fullSizeImg = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/Upload/users/anon.JPG"));
        }


        FileName = filename; //url.Substring(url.LastIndexOf("\\") + 1);

        h = fullSizeImg.Height;
        w = fullSizeImg.Width;

        ratio = ((decimal)h / w);
        w = maxW;
        h = (int)(ratio * maxW);
        if (h > maxH)
        {
            dif = h - maxH;
        }
        h = h - dif;
        w = (int)(h / ratio);
        if (w > maxW)
        {
            dif = w - maxW;
        }
        h = (int)(w * ratio);
        Bitmap b = new Bitmap(w, h);
        Graphics g = Graphics.FromImage((Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(fullSizeImg, 0, 0, w, h);
        //b.Save(HttpContext.Current.Server.MapPath(DestinationFolder + "/tn/" + FileName));
        b.Save(HttpContext.Current.Server.MapPath(DestinationFolder + "/" + FileName));
        fullSizeImg.Dispose();
        g.Dispose();
        //try
        //{
        //    HttpContext.Current.Response.Clear();
        //    HttpContext.Current.Response.Flush();
        //}
        //catch (Exception ex)
        //{
        //}
        return true;
    }
    private static bool ThumbnailCallback()
    {
        return false;
    }

    public static Bintje.Image FromUrlToBintjeImage(string url)
    {
        Image img = Image.FromFile(url);
        Bintje.Image image = new Bintje.Image();
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        image.ImageData = ms.ToArray();
        image.Height = img.Height;
        image.Width = img.Width;
        return image;
    }

    /*
     * TODO: This function it is not necessary anymore
    public static Boolean SendMail(string senderName, string senderEmail, string recipientEmail, string subject, string body)
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(senderEmail, recipientEmail, subject, body);
        msg.IsBodyHtml = true;

        SmtpClient client = new SmtpClient(Convert.ToString(ConfigurationSettings.GetConfig("SMTPServer")));

        // SmtpClient client = new SmtpClient(Convert.ToString(System.Configuration.ConfigurationSettings.GetConfig("SMTPServer")));
        client.Host = System.Configuration.ConfigurationSettings.AppSettings["SMTPServer"];
        client.Port = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["Port"]);
        string fromEmail = System.Configuration.ConfigurationSettings.AppSettings["FromEmail"];
        string fromPassword = System.Configuration.ConfigurationSettings.AppSettings["FromPwd"];
        System.Net.NetworkCredential nCredentials = new System.Net.NetworkCredential(fromEmail, fromPassword);
        client.UseDefaultCredentials = false;
        client.Credentials = nCredentials;
        try
        {
            client.Send(msg);
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
     */
}
